package com.RMP
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.media.*;
    import flash.net.*;
    import flash.text.*;
    import com.greensock.*;

    public class AudioShell extends Sprite
    {
        public var thebar:MovieClip;
        public var channel:SoundChannel;
        public var item:Object;
        public var song:Sound;
        public var position:Number;
        var fadeSpeed:Number;
        var _scrubbing:Boolean;
        var fieldFormat:TextFormat;
        var myRoot:MovieClip;
        var bufferRatio:Number = 0.5;
        public var sndTrans:SoundTransform;
        var styler:StyleSheet;

        public function AudioShell(param1:MovieClip, param2:Number) : void
        {
            bufferRatio = 0.5;
            myRoot = param1;
            fadeSpeed = param2;
            thebar.alpha = 0;
            thebar.visible = false;
            position = 0;
            sndTrans = new SoundTransform(1);
            thebar.progress.width = 0;
            thebar.loaded.addEventListener(MouseEvent.CLICK, seekTo);
            thebar.loaded.buttonMode = true;
            thebar.loaded.useHandCursor = false;
            thebar.progress.mouseEnabled = false;
            thebar.playPause.buttonMode = true;
            thebar.playPause.addEventListener(MouseEvent.MOUSE_OVER, playPauseOver);
            thebar.playPause.addEventListener(MouseEvent.MOUSE_OUT, playPauseOut);
            thebar.playPause.addEventListener(MouseEvent.CLICK, playPauseClick);
            thebar.lbBtn.visible = false;
            fieldFormat = new TextFormat();
            fieldFormat.font = new HelveticaLightEmbed().fontName;
            fieldFormat.leading = 0;
            fieldFormat.size = 14;
            fieldFormat.letterSpacing = 0;
            thebar.scrubber.buttonMode = true;
            _scrubbing = false;
            thebar.scrubber.addEventListener(MouseEvent.MOUSE_DOWN, scrubberPress);
            fieldFormat = new TextFormat();
            fieldFormat.font = new HelveticaLightEmbed().fontName;
            fieldFormat.leading = 0;
            fieldFormat.size = 14;
            fieldFormat.letterSpacing = 0;
            thebar.btnField.autoSize = "left";
            thebar.btnField.defaultTextFormat = fieldFormat;
            thebar.timecode.autoSize = "left";
            thebar.timecode.defaultTextFormat = fieldFormat;
            myRoot.stage.addEventListener(Event.RESIZE, onResizer, false, 0, true);
            doResize();
            return;
        }

        public function playAudio() : void
        {
            position = 0;
            thebar.progress.width = 0;
            thebar.scrubber.x = thebar.progress.width;
            thebar.timecode.text = getTimecode(0);
            item.playing = true;
            song = new Sound();
            song.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
            song.load(new URLRequest(item.data));
            channel = song.play();
            channel.soundTransform = new SoundTransform(0.1);
            channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
            addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
            return;
        }

        public function fadeOutShell() : void
        {
            killAudio();
            TweenMax.to(this, fadeSpeed, {alpha:0, overwrite:true, onComplete:function ()
            {
                visible = false;
                return;
            }
            });
            return;
        }

        function scrubberPress(event:MouseEvent) : void
        {
            var _loc_2:Rectangle = null;
            if (song.bytesLoaded / song.bytesTotal > bufferRatio)
            {
                _scrubbing = true;
                _loc_2 = new Rectangle(0, -5, thebar.track.width, 0);
                thebar.scrubber.startDrag(false, _loc_2);
                stage.addEventListener(MouseEvent.MOUSE_UP, scrubberRelease);
            }
            return;
        }

        function playPauseOver(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.hotColor, overwrite:true});
            if (thebar.playPause.pauseBtn.alpha == 1)
            {
                thebar.btnField.text = "Pause";
            }
            else
            {
                thebar.btnField.text = "Play";
            }
            return;
        }

        function doResize() : void
        {
            thebar.x = 0;
            thebar.y = Math.round(myRoot.headerH + 4 * myRoot.unitH + 3 * myRoot.gutterH);
            thebar.track.width = Math.round(myRoot.gridW);
            var _loc_1:* = myRoot.unitW / 75;
            thebar.timecode.scaleY = myRoot.unitW / 75;
            thebar.timecode.scaleX = _loc_1;
            var _loc_1:* = myRoot.unitW / 75;
            thebar.btnField.scaleY = myRoot.unitW / 75;
            thebar.btnField.scaleX = _loc_1;
            thebar.timecode.x = Math.round(thebar.track.width - thebar.timecode.width + 2);
            var _loc_1:* = myRoot.unitW / 75;
            thebar.caption.scaleY = myRoot.unitW / 75;
            thebar.caption.scaleX = _loc_1;
            thebar.caption.y = Math.round(23 + 29 * (myRoot.unitW / 75));
            if (song != null)
            {
                thebar.loaded.width = thebar.track.width * (song.bytesLoaded / song.bytesTotal);
                if (song.bytesLoaded / song.bytesTotal > bufferRatio)
                {
                    thebar.btnField.x = 15;
                    if (channel != null)
                    {
                        if (item.playing == true)
                        {
                            if (_scrubbing == false)
                            {
                                thebar.progress.width = thebar.track.width * (channel.position / song.length);
                                thebar.scrubber.x = thebar.progress.width;
                                thebar.timecode.text = getTimecode(channel.position / 1000);
                            }
                            else
                            {
                                position = song.length * (thebar.scrubber.x / thebar.track.width);
                                channel.stop();
                                thebar.progress.width = thebar.scrubber.x;
                                thebar.timecode.text = getTimecode(position / 1000);
                            }
                        }
                        else if (_scrubbing == false)
                        {
                            position = song.length * (thebar.scrubber.x / thebar.track.width);
                            thebar.progress.width = thebar.scrubber.x;
                            thebar.timecode.text = getTimecode(position / 1000);
                        }
                    }
                }
                else
                {
                    thebar.btnField.x = -2;
                }
            }
            return;
        }

        function seekTo(event:Event) : void
        {
            if (song.bytesLoaded / song.bytesTotal > bufferRatio)
            {
                channel.stop();
                thebar.scrubber.x = thebar.mouseX;
                thebar.progress.width = thebar.scrubber.x;
                position = song.length * (thebar.scrubber.x / thebar.track.width);
                if (item.playing == true)
                {
                    trace("yes");
                    channel = song.play(position);
                    channel.soundTransform = new SoundTransform(1);
                    channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
                }
            }
            return;
        }

        function killAudio()
        {
            item.playing = false;
            channel.stop();
            song.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            channel.removeEventListener(Event.SOUND_COMPLETE, soundComplete);
            song = null;
            channel = null;
            removeEventListener(Event.ENTER_FRAME, loop);
            return;
        }

        function onResizer(event:Event)
        {
            doResize();
            return;
        }

        private function ioErrorHandler(event:Event) : void
        {
            trace("ioErrorHandler: " + event);
            return;
        }

        private function soundComplete(event:Event) : void
        {
            position = 0;
            channel = song.play(position);
            channel.stop();
            item.playing = false;
            thebar.progress.width = 0;
            thebar.scrubber.x = thebar.progress.width;
            thebar.timecode.text = getTimecode(0);
            thebar.playPause.pauseBtn.alpha = 0;
            thebar.playPause.playBtn.alpha = 1;
            return;
        }

        public function loadAudio(param1:String, param2:String) : void
        {
            thebar.caption.htmlText = param2;
            item = new Object();
            item.data = param1;
            item.playing = true;
            song = new Sound();
            song.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
            song.load(new URLRequest(myRoot.path + item.data));
            channel = song.play();
            channel.soundTransform = new SoundTransform(0.1);
            channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
            addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
            fadeInBar();
            return;
        }

        function playPauseClick(event:MouseEvent) : void
        {
            if (item.playing == true)
            {
                thebar.playPause.pauseBtn.alpha = 0;
                thebar.playPause.playBtn.alpha = 1;
                thebar.btnField.text = "Play";
                item.playing = false;
                position = song.length / (song.bytesLoaded / song.bytesTotal) * (thebar.scrubber.x / thebar.track.width);
                channel.stop();
                thebar.progress.width = thebar.scrubber.x;
                thebar.timecode.text = getTimecode(position / 1000);
            }
            else
            {
                thebar.playPause.pauseBtn.alpha = 1;
                thebar.playPause.playBtn.alpha = 0;
                thebar.btnField.text = "Pause";
                item.playing = true;
                channel = song.play(position);
                channel.soundTransform = new SoundTransform(1);
                channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
            }
            return;
        }

        function fadeInBar() : void
        {
            thebar.visible = true;
            TweenMax.to(thebar, fadeSpeed, {alpha:1, overwrite:true});
            return;
        }

        function loop(event:Event) : void
        {
            thebar.track.width = Math.round(myRoot.gridW);
            thebar.loaded.width = thebar.track.width * (song.bytesLoaded / song.bytesTotal);
            if (song.bytesLoaded / song.bytesTotal > bufferRatio)
            {
                if (thebar.btnField.text == "Buffering audio...")
                {
                    thebar.btnField.text = "";
                    doResize();
                    thebar.playPause.visible = true;
                    thebar.timecode.visible = true;
                    thebar.scrubber.visible = true;
                    TweenMax.to(thebar.playPause, fadeSpeed, {alpha:1, overwrite:true});
                    TweenMax.to(thebar.timecode, fadeSpeed, {alpha:1, overwrite:true});
                    TweenMax.to(thebar.scrubber, fadeSpeed, {alpha:1, overwrite:true});
                    item.playing = true;
                    channel = song.play(0);
                    channel.soundTransform = new SoundTransform(1);
                    channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
                }
                if (channel != null)
                {
                    if (item.playing == true)
                    {
                        if (_scrubbing == false)
                        {
                            thebar.progress.width = thebar.track.width * (channel.position / song.length);
                            thebar.scrubber.x = thebar.progress.width;
                            thebar.timecode.text = getTimecode(channel.position / 1000);
                        }
                        else
                        {
                            position = song.length * (thebar.scrubber.x / thebar.track.width);
                            channel.stop();
                            thebar.progress.width = thebar.scrubber.x;
                            thebar.timecode.text = getTimecode(position / 1000);
                        }
                    }
                    else if (_scrubbing == false)
                    {
                        position = song.length * (thebar.scrubber.x / thebar.track.width);
                        thebar.progress.width = thebar.scrubber.x;
                        thebar.timecode.text = getTimecode(position / 1000);
                    }
                    else
                    {
                        thebar.progress.width = thebar.scrubber.x;
                        position = song.length * (thebar.scrubber.x / thebar.track.width);
                        thebar.timecode.text = getTimecode(position / 1000);
                    }
                }
            }
            else
            {
                if (item.playing == true)
                {
                    item.playing = false;
                    position = 0;
                    channel.stop();
                }
                if (thebar.btnField.text == "")
                {
                    thebar.btnField.text = "Buffering audio...";
                    thebar.btnField.x = -2;
                    thebar.timecode.text = getTimecode(channel.position / 1000);
                }
                thebar.playPause.visible = false;
                thebar.playPause.alpha = 0;
                thebar.timecode.visible = false;
                thebar.timecode.alpha = 0;
                thebar.scrubber.visible = false;
                thebar.scrubber.alpha = 0;
            }
            thebar.timecode.x = Math.round(thebar.track.width - thebar.timecode.width + 2);
            return;
        }

        function getTimecode(param1) : String
        {
            var _loc_2:* = undefined;
            var _loc_3:* = undefined;
            var _loc_4:* = undefined;
            var _loc_5:* = undefined;
            var _loc_6:* = undefined;
            _loc_2 = param1;
            _loc_3 = Math.floor(_loc_2 / 60);
            _loc_4 = Math.floor(_loc_3 / 60);
            _loc_5 = Math.round(_loc_2 - _loc_3 * 60);
            if (_loc_5 == 60)
            {
                _loc_5 = "00";
                _loc_3 = _loc_3 + 1;
                if (_loc_3 < 10)
                {
                    _loc_3 = "0" + _loc_3;
                }
                if (_loc_4 < 10)
                {
                    _loc_4 = "0" + _loc_4;
                }
            }
            else
            {
                if (_loc_5 < 10)
                {
                    _loc_5 = "0" + _loc_5;
                }
                if (_loc_3 < 10)
                {
                    _loc_3 = "0" + _loc_3;
                }
                if (_loc_4 < 10)
                {
                    _loc_4 = "0" + _loc_4;
                }
            }
            _loc_6 = _loc_4 + ":" + _loc_3 + ":" + _loc_5;
            return _loc_6;
        }

        function playPauseOut(event:MouseEvent) : void
        {
            TweenMax.to(event.currentTarget, fadeSpeed, {tint:myRoot.coldColor, overwrite:true});
            thebar.btnField.text = "";
            return;
        }

        function scrubberRelease(event:MouseEvent) : void
        {
            if (song.bytesLoaded / song.bytesTotal > bufferRatio)
            {
                thebar.scrubber.stopDrag();
                _scrubbing = false;
                stage.removeEventListener(MouseEvent.MOUSE_UP, scrubberRelease);
                if (item.playing == true)
                {
                    channel = song.play(position);
                    channel.soundTransform = new SoundTransform(1);
                    channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
                }
            }
            return;
        }

        public function fadeInShell() : void
        {
            visible = true;
            TweenMax.to(this, fadeSpeed, {alpha:1, overwrite:true});
            return;
        }

        function buildStyles() : void
        {
            var _loc_1:* = undefined;
            var _loc_2:* = undefined;
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_7:* = undefined;
            var _loc_8:Object = null;
            _loc_1 = new HelveticaLightEmbed().fontName;
            _loc_2 = new HelveticaBoldEmbed().fontName;
            styler = new StyleSheet();
            _loc_3 = new Object();
            _loc_3.color = myRoot.coldColorHTML;
            _loc_3.fontFamily = _loc_1;
            _loc_3.leading = -4;
            _loc_4 = new Object();
            _loc_4.color = myRoot.coldColorHTML;
            _loc_4.textDecoration = "underline";
            _loc_5 = new Object();
            _loc_5.color = myRoot.hotColorHTML;
            _loc_5.textDecoration = "underline";
            _loc_6 = new Object();
            _loc_6.color = myRoot.coldColorHTML;
            _loc_6.fontFamily = _loc_2;
            _loc_7 = new HelveticaItalicEmbed().fontName;
            _loc_8 = new Object();
            _loc_8.color = myRoot.coldColorHTML;
            _loc_8.fontFamily = _loc_7;
            styler.setStyle(".myItalic", _loc_8);
            styler.setStyle("p", _loc_3);
            styler.setStyle("a:link", _loc_4);
            styler.setStyle("a:hover", _loc_5);
            styler.setStyle(".mybold", _loc_6);
            thebar.caption.autoSize = "left";
            thebar.caption.multiline = true;
            thebar.caption.wordWrap = true;
            thebar.caption.styleSheet = styler;
            return;
        }

    }
}
